package com.momo.auth.pojo;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@TableName("user")
public class UserBo implements UserDetails {

    @TableField("id")
    private Long id;
    @TableField(exist = false)
    private Long user_id;
    @TableField("username")
    private String username;
    @TableField("password")
    private String password;
    @TableField("auth")
    private String auth;
    @TableField("sex")
    private String sex;
    @TableField("phone")
    private String phone;

    public void setId(Long id) {
        this.id = id;
        this.user_id = Long.valueOf(id);
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        String[] split = auth.split(",");
        List<SimpleGrantedAuthority> authorityList = new ArrayList<>();
        for (String code:
                split) {
            authorityList.add(new SimpleGrantedAuthority(code));
        }
        return authorityList;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return false;
    }

    @Override
    public boolean isAccountNonLocked() {
        return false;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return false;
    }

    @Override
    public boolean isEnabled() {
        return false;
    }
}
